#!/bin/bash

# 默认构建类型
build_type="--debug"
build_opt="--overwrite"
toolchain_file="./toolchain.cmake"

# 定义颜色
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
RESET="\033[0m"  # 重置颜色

# 打印帮助信息
show_help() {
    echo "Usage: $0 [OPTION] [BUILD_TYPE]"
    echo ""
    echo "Options:"
    echo "  --rebuild       Rebuild the project from scratch."
    echo "  --overwrite     Overwrite the build directory and build."
    echo ""
    echo "BUILD_TYPE:"
    echo "  --debug         Build the project in Debug mode (default)."
    echo "  --release       Build the project in Release mode."
    echo ""
    echo "Use --help to display this help message."
}

# 检查参数数量并处理
if [ $# -gt 2 ]; then
    echo "Invalid number of arguments."
    show_help
    exit 1
fi

# 处理第一个参数，构建选项
if [ $# -ge 1 ]; then
    case "$1" in
        --rebuild)
            build_opt="rebuild"
            ;;
        --overwrite)
            build_opt="overwrite"
            ;;
        --help)
            show_help
            exit 0
            ;;
        *)
            echo "Invalid option: $1."
            show_help
            exit 1
            ;;
    esac
fi

# 处理第二个参数，指定构建类型
if [ $# -eq 2 ]; then
    case "$2" in
        --debug)
            build_type="debug"
            ;;
        --release)
            build_type="release"
            ;;
        *)
            echo "Invalid build type: $2. Options are --debug or --release."
            show_help
            exit 1
            ;;
    esac
fi

# 构建操作
build_dir="build"
if [ "$build_opt" == "rebuild" ] && [ -d "$build_dir" ]; then
    rm -rf "$build_dir"
    echo -e "${BLUE}***************0.rebuild***************${RESET}"
fi

# 定义 CMake 参数
cmake_args=(
    -DCMAKE_TOOLCHAIN_FILE="$toolchain_file"
    -DCMAKE_BUILD_TYPE="${build_type}"  # 从 --debug 或 --release 
    -Wno-dev                                 # 忽略开发者相关的警告
)
echo "cmake_args: ${cmake_args[@]}"

mkdir -p "$build_dir"
(
    cd "$build_dir" || exit
    echo -e "${BLUE}***************1.cmake***************${RESET}"
    cmake "${cmake_args[@]}" ..
    echo -e "${BLUE}***************2.make***************${RESET}"
    make
)
echo -e "${BLUE}***************3.copy***************${RESET}"
cp -rfv ./bin/* /home/joke/nfs
